package com.spring.sunflower.common

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseFragment
import com.spring.sunflower.bean.*

/**
 * Created by Kevin on 2021/8/27<br/>
 *
 * Describe:<br/>
 */
class IncomeChatFragment : BaseFragment<IncomePresenter>(), CommonContract.IncomeView,
    OnLoadMoreListener, OnRefreshListener {
    private var refreshLayout: SmartRefreshLayout? = null
    private var rvRecyclerView: RecyclerView? = null
    private var mAdapterGift: IncomeGiftAdapter? = null
    private var mAdapterIM: IncomeChatAdapter? = null
    private var mAdapterVideoChat: IncomeVideoChatAdapter? = null
    private var mDataGift: MutableList<IncomeGiftBean.DatasBean> = ArrayList()
    private var mDataIM: MutableList<IncomeChatBean.DatasBean> = ArrayList()
    private var mDataVideoChat: MutableList<IncomeVideoChatBean.DatasBean> = ArrayList()
    private var pageIndexGift = 1
    private var pageIndexIM = 1
    private var pageIndexVideoChat = 1
    private var llEmpty: LinearLayout? = null
    private var type: String = ""

    companion object {
        fun newInstance(type: String): IncomeChatFragment {
            val args = Bundle()
            args.putString("type", type)
            val fragment = IncomeChatFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun setLayoutResId(): Int = R.layout.fragment_sub_award

    override fun initView() {
        llEmpty = mView.findViewById(R.id.llEmpty)
        refreshLayout = mView.findViewById(R.id.refreshLayout)
        rvRecyclerView = mView.findViewById(R.id.rvRecyclerView)
        mAdapterGift = IncomeGiftAdapter(R.layout.adapter_item_award, mDataGift)
        mAdapterIM = IncomeChatAdapter(R.layout.adapter_item_award, mDataIM)
        mAdapterVideoChat = IncomeVideoChatAdapter(R.layout.adapter_item_award, mDataVideoChat)
        val linearLayoutManager = LinearLayoutManager(mActivity)
        rvRecyclerView!!.layoutManager = linearLayoutManager
        rvRecyclerView!!.adapter = mAdapterIM
        refreshLayout!!.setOnRefreshListener(this)
        refreshLayout!!.setOnLoadMoreListener(this)

    }

    override fun initData() {}

    override fun initListener() {
    }

    override fun loadData() {
        pageIndexIM = 1
        mPresenter.getChatIncome(pageIndexIM.toString())
    }

    override fun setPresenter(): IncomePresenter = IncomePresenter(this)

    override fun onLoadMore(refreshLayout: RefreshLayout) {

        mPresenter.getChatIncome((++pageIndexIM).toString())

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        pageIndexIM = 1
        mPresenter.getChatIncome(pageIndexIM.toString())

    }

    override fun showIncomeGift(bean: MutableList<IncomeGiftBean.DatasBean>?) {
    }

    override fun showIncomeChat(bean: MutableList<IncomeChatBean.DatasBean>?) {
        if (pageIndexIM == 1) {
            if (bean.isNullOrEmpty()) {
                llEmpty!!.visibility = View.VISIBLE
                refreshLayout!!.visibility = View.GONE
            } else {
                llEmpty!!.visibility = View.GONE
                refreshLayout!!.visibility = View.VISIBLE
                mAdapterIM!!.setList(bean)
            }
        } else {
            mAdapterIM!!.addData(bean!!)
        }
        if (refreshLayout!!.isLoading) {
            refreshLayout!!.finishLoadMore()
        }
        if (refreshLayout!!.isRefreshing) {
            refreshLayout!!.finishRefresh()
        }
    }

    override fun showIncomeVideoChat(bean: MutableList<IncomeVideoChatBean.DatasBean>?) {
    }
}